package basics.day16.DateDemo;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
    public static void main(String[] args) throws Exception {
        // fun_1();
//        fun_2();
        // fun_3();
        fun_4();
    }

    /*
     * 字符串转换成日期
     * */
    public static void fun_4() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse("2089-10-1");
        System.out.println(date);
    }

    /*
     * 格式化日期SimpleDateFormat
     * */
    public static void fun_3() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(new Date());
        System.out.println(date); // 2019-02-13 17:01:02
    }

    /*
     * 毫秒转换成日期 setTime()
     * */
    public static void fun_2() {
        Date date = new Date();
        date.setTime(0);
        System.out.println(date); //  Thu Jan 01 08:00:00 CST 1970

        // Date的构造方法也可以实现setTime
        Date date1 = new Date(0);
        System.out.println(date1); // Thu Jan 01 08:00:00 CST 1970
    }

    /*
     * 获取日期的毫秒值 getTime
     * */
    public static void fun_1() {
        Date date = new Date();
        System.out.println("date：" + date);
        long time = date.getTime();
        System.out.println("getTime：" + time);
    }
}
